package com.jt.www.util;

import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author admin_ltf
 * @create 2019-04-13 10:34
 */
public class JsonUtils {

    public static <T> T fromJson(String paramString, Class<T> paramClass) {
        return new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create().fromJson(paramString, paramClass);
    }

    public static String toJson(Object paramObject) {
        paramObject = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create().toJson(paramObject);
        return (String) paramObject;
    }

    /**
     * 读取json文件
     */
    public static String parseJsonFile(String path) {
        String jsonStr = "";
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(new File(path)));
            String temp = null;
            int lineNum = 1;
            while ((temp = reader.readLine()) != null) {
                jsonStr = jsonStr + temp;
                lineNum++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

}
